package com.qtu.user_service.aop;

import com.netflix.zuul.context.RequestContext;
import com.qtu.entity.BmMember;
import com.qtu.user_service.feign.SellerFeignService;
import com.qtu.util.CacheUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 切面类：验证token
 */
@Slf4j
@Aspect
@Component
public class TokenAspect {
    @Autowired
    private SellerFeignService sellerFeignService;

    //切入点
    @Pointcut("execution(* com.qtu.user_service.controller.*.*(..)) && !execution(* com.qtu.user_service.controller.SellerController.getLogin(..)) && !execution(* com.qtu.user_service.controller.SellerController.register(..))")
    public void TokenAspect(){}

    @Before("TokenAspect()")
    public void isExistTokenByBefore(){
        try {
            //1、获取 request
            ServletRequestAttributes requestAttributes = (ServletRequestAttributes)
                    RequestContextHolder.getRequestAttributes();
            HttpServletRequest request = requestAttributes.getRequest();
            HttpServletResponse response = requestAttributes.getResponse();
            //2、获取 token
            String token = request.getParameter("token");

            String exists = sellerFeignService.exists(token);
            if(!StringUtils.equals("true",exists)){
                //3、登录信息过期
                RequestContext context = RequestContext.getCurrentContext();
                context.setSendZuulResponse(false);
                context.setResponseStatusCode(401);
                return;
            }
        } catch (Exception e) {
            log.error("@Before执行失败...："+e);
        }
    }
    @After("TokenAspect()")
    public void isExistTokenByAfter(){
        try {
            //1、获取 request
            ServletRequestAttributes requestAttributes = (ServletRequestAttributes)
                    RequestContextHolder.getRequestAttributes();
            HttpServletRequest request = requestAttributes.getRequest();
            HttpServletResponse response = requestAttributes.getResponse();
            //2、获取 token
            String token = request.getParameter("token");

            String exists = sellerFeignService.exists(token);
            if(!StringUtils.equals("true",exists)){
                //3、登录信息过期import com.netflixzuul.contextR.equestContext;
                RequestContext context = RequestContext.getCurrentContext();
                context.setSendZuulResponse(false);
                context.setResponseStatusCode(401);
                return;
            }
            //4、刷新
            Object obj = sellerFeignService.get(token);
            BmMember member = (BmMember) CacheUtil.linkedHashMapToObject(obj, BmMember.class);
            sellerFeignService.setByTime(token,member,60);
        } catch (Exception e) {
            log.error("@After执行失败...："+e);
        }
    }

}
